package com.hjq.fcloud.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.CheckBox
import com.alibaba.fastjson.JSON
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.internal.LinkedTreeMap
import com.hjq.fcloud.R
import com.hjq.toast.ToastUtils

/**
 * 列表选择，主要是选择
 */
class SearchTreeActivity : CommonListActivity() {

    companion object {
        //多传过去的参数
        const val INTENT_IN_REQUEST_MAP = "request_map"
        //数据回传key
        const val INTENT_RESULT_DATA = "intent_result_data"

        fun getLauncherIntent(
            context: Context,
            id: String,
            requestMap: HashMap<String,String>? = null
        ): Intent? {
            if (TextUtils.isEmpty(id)) {
                ToastUtils.show("menu_id不能为空")
                return null
            }
            var intent = Intent(context, SearchTreeActivity::class.java)
            intent.putExtra(INTENT_COMMON_LIST_KEY_ID, id)
            requestMap?.let{
                intent.putExtra(INTENT_IN_REQUEST_MAP, requestMap)
            }
            return intent
        }
    }

    private val requestMap by lazy{ getSerializable(INTENT_IN_REQUEST_MAP) as? HashMap<String,String> }

    override fun initData() {
        super.initData()
        addSaveMenu()
    }

    private fun addSaveMenu() {
        mTitleBar?.setRightTitle("保存")
        mTitleBar?.setRightTitleColor(resources.getColor(R.color.common_color_blue))
        mTitleBar?.rightView?.setOnClickListener(View.OnClickListener {
            selectPos?.let { selectPos ->
                var selectItem = mList?.get(selectPos)

                var intent = Intent()
                intent.putExtra(INTENT_RESULT_DATA, JSON.toJSONString(selectItem))
                setResult(Activity.RESULT_OK, intent)
                finish()
            } ?: let {
                showToast("请选择")
            }
        })
    }

    private var selectPos: Int? = null

    override fun handlerOtherView(
        helper: BaseViewHolder, obj: LinkedTreeMap<String, Any>
    ) {
        var checkBox = helper.getView<CheckBox>(R.id.id_check)
        checkBox.visibility = View.VISIBLE
        var position = helper.layoutPosition
        checkBox.isSelected = (selectPos != null && position == selectPos)
        helper.getConvertView().setOnClickListener {
            selectPos = position
            mAdapter?.notifyDataSetChanged()
        }
    }

    /**
     * 不需要跳转详情
     */
    override fun jumpDetail(item: LinkedTreeMap<String, Any>, startSign: Boolean) {
    }

    override fun getServerListData(isLoadMore: Boolean) {
        super.getServerListData(isLoadMore)
        selectPos = null
    }

    override fun getQueryOrderMap(): java.util.HashMap<String, String> {
        var map = super.getQueryOrderMap()
        if(requestMap != null){
            map.putAll(requestMap!!)
        }
        return map
    }
}